<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addUser">添加用户</button>
        <button class="layui-btn layui-btn-sm" lay-event="resetPass">重置密码</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-bg-black" lay-event="setRole">设置角色</a>
</script>


<script  src="../../../layui/layui.js" ></script>
<script src="../../../layui/layui.all.js"  ></script>
<script src="../../../js/jquery.min.js"></script>

<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#test'
            ,url:'/sysUser/layui'
            ,method: 'post',
            contentType: 'application/json',
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            where: {
                "orderBy": "id asc",
            }
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'username', title:'登录名', width:120}
                ,{field:'nickname', title:'昵称', width:120}
                ,{field:'phone', title:'手机号码', width:120}
                ,{field:'email', title:'邮箱', width:150,  templet: function(res){
                        return '<em>'+ res.email +'</em>'
                    }}
                ,{field:'sex', title:'性别', width:80, templet: function(res){
                    if(res.sex ==1){
                        return '<em>男</em>'
                    }else{
                        return '<em>女</em>'
                    }
                    }}
                ,{field:'localip', title:'IP', width:120}
                ,{filed: 'status',title:'启用状态',templet:'#statusTemp'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:260}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'addUser':
                    layer.open({
                        type: 2,
                        shadeClose: false,
                        shade: 0.8,
                        area: ['30%', '75%'],
                        content: '/views/system/user/userAdd.html' //iframe的url
                    });
                    break;
                case 'resetPass':
                    var data = checkStatus.data;
                    if(data.length == 0){
                        layer.msg("请至少勾选一个数据！");
                    }else {
                        var userId=[];
                        data.forEach(function(e){
                            userId.push(e.id)
                        });
                        resetPass({
                            userId: userId
                        });

                    }
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });
        form.on('switch(status)', function(data){
            // 得到开关的value值，实际是需要修改的ID值。
            var id = data.value;
            var status = this.checked ? '1' : '0';
            updateStatus({
                userID: id,
                status: status
            });

        });

        //启用/禁用
        function updateStatus(uploadData){
            $.ajax({
                type : 'put',
                url : '/sysUser/editStatusById',
                data : uploadData,
                async:false,
                success : function(data) {
                    layer.msg(data.msg);

                }
            });
        }

        //重置密码
        function resetPass(userId){

            $.ajax({
                type : 'post',
                url : '/sysUser/resetPass',
                contentType: "application/json; charset=utf-8",
                data : JSON.stringify(userId),
                success : function(data) {
                    layer.msg(data.msg);
                    location.reload();
                }
            });
        }

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('删除该用户？', function(index){
                    $.ajax({
                        type: 'DELETE',
                        url: '/sysUser/delUser',
                        data: {"userId" :data.id},
                        success: function(data){
                            if(data.code == 1){
                                layer.msg('操作成功！');
                                layer.close(index);
                                setTimeout(function(){
                                    location.reload();
                                },1000 );
                            }else{
                                layer.msg('数据异常，操作失败！');
                            }},

                    });
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2,
                    shadeClose: false,
                    shade: 0.8,
                    area: ['30%', '75%'],
                    content: '/views/system/user/userEdit.html?userId='+data.id
                });

            }
            else if(obj.event === 'setRole'){
                layer.open({
                    type: 2,
                    shadeClose: false,
                    shade: 0.8,
                    area: ['30%', '55%'],
                    content: '/views/system/user/userSetRole.html?userId='+data.id+'&username='+data.username
                });
            }
        });
    });
</script>
<script type="text/html" id="statusTemp">
    {{#  if(d.status==1){ }}
    <input type="checkbox" name="status" lay-skin="switch"   lay-text="开启|关闭"  value= "{{ d.id}}"  lay-filter="status" checked="true" />
    {{#  } else { }}
    <input type="checkbox" name="status" lay-skin="switch"  lay-text="开启|关闭"  value=" {{ d.id}}" lay-filter="status" />
    {{#  } }}
</script>
</body>
</html>